import * as API from '../../api/shareApi';
import * as util from '../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    dataSource: [],
    shareId: '',
    customName: '',
    companyName: '',
    extra_data: {
      key: '',
      shareId: ''
      // shareInfo: {
      //   customId: '',
      //   shardingId: '',
      //   companyName: '',
      //   userId: '',
      //   sharePage: 'shareDetail',
      //   orderId: '',
      //   shareId: '',
      //   customName: ''
      // }
    },
    navigator_1: {
      title: '快速体验',
      tag: '企业端'
    }
  },
  setKey: function () {
    setTimeout(() => {
      this.setData({
        'extra_data.key': new Date().getTime()
      });
    }, 100)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.shareId) {
      this.setData({
        shareId: options.shareId
      });
      this.loadData(options.shareId)
    }
  },
  loadData: function (shareId) {
    API.getOrdersByShareId({ shareId: shareId }).then(res => {
      if (res.orderList) {
        this.setData({
          dataSource: res.orderList,
          total: res.orderList.length,
          customName: res.orderList && res.orderList[0].customName,
          companyName: res.companyName
        })
        // let shareInfo = {
        //   companyName: res.companyName,
        //   shareId: options.shareId,
        //   shardingId: res.orderList && res.orderList[0].shardingId,
        //   customId: res.orderList && res.orderList[0].customId,
        //   customName: res.orderList && res.orderList[0].customName,
        //   userId: res.shareContent && res.shareContent.userId,
        //   sharePage: 'shareList'
        // }
        // this.setData({
        //   'extra_data.shareInfo': shareInfo
        // });
        wx.stopPullDownRefresh();
        this.setData({
          'extra_data.shareId': shareId
        });
      }
    }, err => {
      if (err == '已过期') {
        wx.redirectTo({
          url: '/pages/expire/expire'
        })
      };
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      'extra_data.key': new Date().getTime()
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let shardId = this.data.shareId;
    this.loadData(shardId);
  },
  gotoLogin: function () {
    let token = wx.getStorageSync('token');
    if (token) {
      wx.redirectTo({
        url: '/pages/home/home',
      })
    } else {
      wx.reLaunch({
        url: '/pages/login/login',
      })
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})